package cn.libinal.scloud.base.common.web;

import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
public final class ResultPage<T> {
    private long total;
    private int pageNumber;
    private int pageSize;
    private boolean hasPrevious;
    private boolean hasNext;
    private boolean first;
    private boolean last;
    private List<T> data;
    DataPageRequest previous;
    DataPageRequest next;

    private ResultPage() {

    }

    public static  <T> ResultPage<T> buildPage(Page<T> page) {
        ResultPage<T> resultPage = new ResultPage<>();
        resultPage.setData(page.getContent());
        resultPage.setTotal(page.getTotalElements());
        resultPage.setPageNumber(page.getNumber());
        resultPage.setPageSize(page.getSize());
        resultPage.setHasPrevious(page.hasPrevious());
        resultPage.setHasNext(page.hasNext());
        resultPage.setFirst(page.isFirst());
        resultPage.setLast(page.isLast());
        if (page.hasPrevious()) {
            resultPage.setPrevious(DataPageRequest.of(page.previousPageable()));
        }
        if (page.hasNext()) {
            resultPage.setNext(DataPageRequest.of(page.nextPageable()));
        }
        return resultPage;
    }
}
